package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.config.exception.GuiguException;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dale
 * @title SysRoleController
 * @date 2023/5/30 21:39
 */
@Api(tags = "角色管理接口")
//ResponseBody+Controller=RestController
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
   // http://localhost:8800/admin/system/sysRole/findAll
    //http://localhost:8800/doc.html

    //注入service
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll(){
        List<SysRole> list = sysRoleService.list();

        return Result.ok(list);
    }

    //条件分页查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    //@PathVariable 用于路径传值
    //SysRoleQueryVo类里面只有RoleName一个属性,前端传来的条件查询参数
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo){
        //1.创建一个page对象，用于传递page和limit参数
        Page<SysRole> pageParam = new Page<>(page, limit);
        //2.封装条件，条件可能为空
        LambdaQueryWrapper<SysRole> wrapper=new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)){
            //进行封装,模糊查询roleName
            wrapper.like(SysRole::getRoleName,roleName);
        }
        //3.调用方法，实现分页查询
        IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    //添加角色
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("save")//get没有请求体，如果要用RequestBody传递参数，就得使用Post
    public Result save(@RequestBody SysRole role){
        //调用Service方法
        boolean is_success = sysRoleService.save(role);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

//    @ApiOperation("添加角色")
//    @GetMapping("save")
//    public Result save(SysRole role){
//        //调用Service方法
//        boolean is_success = sysRoleService.save(role);
//        if (is_success){
//            return Result.ok();
//        }else {
//            return Result.fail();
//        }
//    }

    //修改角色-根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //修改角色-最终修改
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("update")//get没有请求体，如果要用RequestBody传递参数，就得使用Post
    public Result update(@RequestBody SysRole role){
        //调用Service方法
        boolean is_success = sysRoleService.updateById(role);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    //根据id删除
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = sysRoleService.removeById(id);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    //批量删除
    //前端数组 [1,2,3,4],后端用list集合接收
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean is_success = sysRoleService.removeByIds(idList);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



}
